<?php
/**
 * Created by 绵阳人维网络科技有限公司.
 * User: 罗东华
 * Date: 2016/4/18
 * Time: 13:46
 */
namespace Admin\Controller;
class ConfigController extends AdminBase{

    public function index(){

        if(IS_POST){
            $res=D('Config','Logic')->update_config();
            if($res){ update_system_config();}//更新系统配置
            $this->ThisReturn($res);
        }

        $id=I('get.id',1,'int');
        $Group=D('Config','Logic')->group();
        $Config=D('Config','Logic')->getConfig($id);
        $this->assign('Group',$Group);
        $this->assign('Config',$Config);
        $this->assign('id',$id);
        $this->display();
    }

    public function config(){
        $keywords=I('get.keywords');
        $id=I('get.id',-1,'int');
        if($id!=-1){
            $where['group']=$id;
        }
        if($keywords){
            $where['title']=array('like',"%$keywords%");
        }

        $Group=D('Config','Logic')->group();
        $this->assign('config_type',C('CONFIG_FIELD_TYPE'));
        $list=$this->Pages('config',$where);
        $this->assign('list',$list);
        $this->assign('Group',$Group);
        $this->assign('id',$id);
        $this->display();
    }
    /*删除配置字段*/
    public function config_del($id){
        if($id){
            if(is_array($id)){
                $where['id']=array('in',$id);
            }else{
                $where['id']=$id;
            }
            $res=DB_del('Config',$where);
            if($res){ update_system_config();}//更新系统配置
            $this->ajaxReturn($res);
        }
    }
    /*编辑配置字段*/
    public function config_edit(){
        $id=I('get.id','','int');
        if(IS_POST){
            if($id){
                $where['id']=$id;
                $res=DB_edit('Config','','','save',$where);
                if($res){ update_system_config();}//更新系统配置
                $this->ThisReturn($res);
            }else{
                $res=DB_edit('Config');
                if($res){ update_system_config();}//更新系统配置
                $this->ThisReturn($res);
            }
        }

        if($id){
            $info= M('Config')->where(array('id'=>$id))->find();
            $this->assign($info);
        }

        $this->assign('config_type',C('CONFIG_FIELD_TYPE'));
        $this->assign('groups',D('Config','Logic')->group());
        $this->assign('id',$id);

        $this->display();
    }
    /*配置字段检查字段是否重复*/
    public function config_check(){
        $id=I('get.id','','int');
        if(IS_AJAX){
            if($id){$where['id']=array('neq',$id);}
            $this->ajaxReturn(field_check('Config',$where));
        }

    }

}